package cn.tedu.jdbc;

import java.sql.*;
import java.util.Scanner;

/**
 * 测试 jdbc
 */
public class Test2 {
    public static void main(String[] args) {
        method();
//        login();
//        login2();
    }
    //需求：cgb2104库里的dept表里插入数据
    private static void method()  {
        Statement st = null;
        Connection conn=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/mydemo1?characterEncoding=utf8";//指定要连接哪个数据库
            conn = DriverManager.getConnection(url, "root","root");
            st = conn.createStatement();
            int rows = st.executeUpdate("insert into a values(null ,'eee','333')");
            ResultSet rr = st.executeQuery("select * from a");
//            System.out.printdln(rows);
            while(rr.next()){
                for (int i = 1; i <= 3; i++) {
                    System.out.print(rr.getString(i)+"\t\t");
                }
                System.out.println();
            }


        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("执行失败....");
        }finally {
            try {
                st.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
     }
    private static void login(){
        System.out.println();
        Scanner input =new Scanner(System.in);
        System.out.println("请输入账号：");
        String user=input.nextLine();
        System.out.println("请输入密码：");
        String pwd=input.nextLine();
        Statement st = null;
        Connection conn=null;
        ResultSet rs=null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydemo1?characterEncoding=utf8", "root","root");
            st = conn.createStatement();
            String sql = "select * from a where name ='"+user+"' and password = '"+pwd+"'";
            rs = st.executeQuery(sql);
            if (rs.next()){
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败！");
            }



        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("执行失败....");
        }finally {
//            JDBCUtils.close(rs,pt,conn);
        }
    }
    private static void login2(){
        System.out.println();
        Scanner input =new Scanner(System.in);
        System.out.println("请输入账号：");
        String user=input.nextLine();
        System.out.println("请输入密码：");
        String pwd=input.nextLine();
//        Statement st = null;
        PreparedStatement ps= null;
        Connection conn=null;
        ResultSet rs=null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydemo1?characterEncoding=utf8", "root","root");
//            st = conn.createStatement();//不安全，会被sql攻击
            //
            String sql = "select * from a where name=? and password=?";
            ps = conn.prepareStatement(sql);
            //给SQL设置参数
            ps.setString(1,user);
            ps.setString(2,pwd);
            rs = ps.executeQuery();
            if (rs.next()){
                System.out.println("登录成功");
            }else{
                System.out.println("登录失败！");
            }



        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("执行失败....");
        }finally {
            JDBCUtils.close(rs,ps,conn);

        }
    }
}
